package org.finesys.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.finesys.auth.api.token.service.AuthTokenService;
import org.finesys.common.core.module.R;
import org.finesys.common.log.core.annotation.LogAnnotation;
import org.finesys.common.log.core.enums.OperateType;
import org.finesys.system.constants.ControllerRestApiConfig;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "令牌管理")
@RestController
@RequiredArgsConstructor
@RequestMapping(ControllerRestApiConfig.TOKEN)
public class SysTokenController {
    private final AuthTokenService authTokenService;

    /**
     * 删除token
     */
    @Operation(summary = "删除token")
    @LogAnnotation(title = "强退", operateType = OperateType.FORCE)
    @SneakyThrows
    @DeleteMapping("/token/remove")
    public R<?> removeToken(@RequestBody String[] tokens) {
        for (String token : tokens) {
            authTokenService.removeToken(token);
        }
        return R.success();
    }

    /**
     * 分页查询token
     *
     * @param params 分页参数
     * @return
     */
    @Operation(summary = "分页查询token")
    @PostMapping("/token/page")
    public R<Page<?>> tokenList(@RequestBody Map<String, Object> params) {
        return authTokenService.tokenPage(params);
    }

    /**
     * 根据Token查询用户信息
     *
     * @param token token
     * @return 用户信息
     */
    @Operation(summary = "根据Token查询用户信息")
    @PostMapping("/token/getUserByToken")
    public R<Map<String, Object>> getUserByToken(@RequestParam("token") String token) {
        return authTokenService.getUserByToken(token);
    }
}
